package com.zhendong.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zhendong.common.valid.AddGroup;
import com.zhendong.common.valid.UpdateGroup;
import com.zhendong.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.zhendong.gulimall.product.entity.BrandEntity;
import com.zhendong.gulimall.product.service.BrandService;
import com.zhendong.common.utils.PageUtils;
import com.zhendong.common.utils.R;


/**
 * 品牌
 *
 * @author zhendong
 * @email zhendong@gmail.com
 * @date 2020-11-09 21:23:51
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }

    //search调用的远程服务
    @GetMapping("/infos")
    public R BrandsInfo(@RequestParam("brandIds") List<Long> brandIds){
        List<BrandEntity> brands = brandService.getBrandsByIds(brandIds);

        return R.ok().put("brand", brands);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * <p>
     * 这里的逻辑原本就这两行，但是返回的结果很乱不整齐，所以把结果获取到封装到R对象里
     * brandService.save(brand);
     * return R.ok();
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand  /*,BindingResult result*/) {
        /*if (result.hasErrors()) {
            Map<String, String> map = new HashMap<>();
            //获取校验的错误结果
            result.getFieldErrors().forEach((item) -> {
                //FieldError获取错误提示
                String field = item.getField();
                String defaultMessage = item.getDefaultMessage();
                map.put(field, defaultMessage);
            });
            return R.error(400,"提交的数据不合法").put("data",map);
        } else {
            brandService.save(brand);
            return R.ok();
        }*/

        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        //brandService.updateById(brand); 这个不行，要更新冗余字段
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改状态【这个接口并不冗余，因为提交update是必须要保证name不能是空串必须校验，而status的接口不需要校验name】
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
